import numpy as np
import pandas as pd

# DataFrame

a = [1, 2, 3, 4, 5, 5]

my_var = pd.Series(a)

print(my_var)

# 服从0-1分布的随机数
data_frame_1 = pd.DataFrame(np.random.rand(3, 5))
print(data_frame_1)

# ndim维度
print("size:", data_frame_1.size, "shape:", data_frame_1.shape, "ndim", data_frame_1.ndim)

data = {"state": ["A", "B", 'C', 'D'],
        "year": [2001, 2002, 2003, 2004],
        "data": [5.4, 3.4, 4.4, 8.6]}

df = pd.DataFrame(data, columns=["year", "state", "data"])

print('-----------------完整数据--------------------')
print(df)

'''
# DataFrame 的属性和方法
print(df.shape)  # 形状
print(df.columns)  # 列名
print(df.index)  # 索引
print(df.head())  # 前几行数据，默认是前 5 行
print(df.tail())  # 后几行数据，默认是后 5 行
print(df.info())  # 数据信息
print(df.describe())  # 描述统计信息
print("mean:", df.mean())  # 求平均值
print("sum:", df.sum())  # 求和
'''

print('-----------------提取多列---------------------')
# 索引和切片
print(df[['year', 'state']])  # 提取多列

print('-----------------切片行1-2行---------------------')
print(df[1:3])  # 切片行

print('-----------------切片 0:3,0:2---------------------')
print(df.iloc[0:3, 0:2])

print('------------------提取单列----------------------------')
print(df.loc[:, 'year'])  # 提取单列

print('-------------------标签索引提取指定行列-------------------')
print(df.loc[1:2, ['year', 'state']])  # 标签索引提取指定行列

print('-------------------位置索引提取指定列-------------------')
print(df.iloc[:, 1:])  # 位置索引提取指定列

print('----------------检索data 大于5的行----------------------')
r = df.loc[(df["data"] > 5), :]
print(r)

# 数据预处理
df3 = pd.DataFrame({"A": [np.random.randint(1, 100) for i in range(4)],
                    "B": np.array([3] * 4, dtype='int32'),
                    "C": [np.nan, "f2", np.nan, "f4"],
                    "D": pd.Series([1, 2, 3, np.nan])})

print('----------------df3----------------------')
print(df3)

print('----------------df3.dropna------移除NAN----------------')
print(df3.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False))

# print('----------------df3.dropna------数据补充 指定位置----------------')
# df3.iat[0, 2] = 'Added data'
# print(df3)

# print('----------------df3.fillna------数据默认值填充缺失的值----------------')
# df3 = df3.fillna(100)
# print(df3)

# d_mean =
print('-----------------------------------------------------')
print(df3)
df3.dropna(subset=['D'], inplace=True)
# df3.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
print('---------------d_mean=df3["D"].mean-----\n',  df3["D"].mean)

print('----------------df3.fillna------均值填充----------------')
df3["D"].fillna(value=2,
                method=None,
                axis=None,
                inplace=True,
                limit=None,
                downcast=None, )

df3["C"].fillna(value='CCCCCCC',
                method=None,
                axis=None,
                inplace=True,
                limit=None,
                downcast=None, )

print('---------------- 拼接----------------')
df3_row2 = df3.loc[(df3["D"] > 2), :]

df4 = pd.concat([df3, df3_row2])
print(df4)
